How to update gcc 4.8 on Raspberry Pi

How to update gcc 4.8 on Raspberry Pi

How to upate gcc 4.8 on Raspberry Pi

This post is mostly come from here.

Before we start, we have to check a couple of things.

Check current gcc version

First we have to check the version of the current gcc.

$ gcc -v

If your version is greater than 4.8, then you don’t have to do anything. That will be the end of this post. If your version is lower than 4.8 then, too bad. We have to check and run a few commands.

Upgrading Respberry Pi Distribution

Now we check the current Raspberry Pi distribution version. We have to be on Wheezy distribution. Follow this link to upgrade Respberry Pi.

Changing source list

Before we do anything, we have to backup the file.

$ sudo cp /etc/apt/sources.list ~/

Now we can modify the soure list file.

$ sudo nano /etc/apt/sources.list

Delete everything in there,copy the following lines and paste it.

deb http://mirrordirector.raspbian.org/raspbian/ wheezy main contrib non-free rpi
deb http://archive.raspbian.org/raspbian wheezy main contrib non-free rpi
# Source repository to add
deb-src http://archive.raspbian.org/raspbian wheezy main contrib non-free rpi
deb http://mirrordirector.raspbian.org/raspbian/ jessie main contrib non-free rpi
deb http://archive.raspbian.org/raspbian jessie main contrib non-free rpi
# Source repository to add
deb-src http://archive.raspbian.org/raspbian jessie main contrib non-free rpi

Note: If you are not comfortable with deleting, you can comment out by using # in the file.

Note: Make sure you save the file by pressing Ctrl + x then press Ctrl + y

Then we create a preference file

$ sudo nano /etc/apt/preferences

Similary, paste it in the file and save.

Package: *
Pin: release n=wheezy
Pin-Priority: 900
Package: *
Pin: release n=jessie
Pin-Priority: 300
Package: *
Pin: release o=Raspbian
Pin-Priority: -10
Installation of gcc

Now we will just run a few command lines to install.

$ sudo apt-get update
$ sudo apt-get install -t jessie gcc-4.8 g++-4.8
$ sudo update-alternatives --remove-all gcc 
$ sudo update-alternatives --remove-all g++
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.6 20
$ sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.8 50
$ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.6 20
$ sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.8 50

Finally, we check whether gcc is successfully updated or not.

$ gcc -v

Now you are happy with freshly new gcc and ready to do another awesome project? Wait, we still need one final touch.

Reverting the source list

Now you can revert it back by typing

$ sudo cp ~/sources.list /etc/apt/

We will be deleting the file that we first created since we no longer need it.

$ sudo rm /etc/apt/preferences
What if I mess up the source list

Note: If you forget to backup, you should at least modify back to Wheezy source list.

Now type,

$ sudo nano /etc/apt/sources.list

Delete everything from there and paste the following line in. Then don’t forget to save the file.

deb http://mirrordirector.raspbian.org/raspbian/ wheezy main contrib non-free rpi

1 comment: