Unable to locate package on Ubuntu Linux when using apt-get

I am tying to install package using the apt or apt-get command line. I get an error that read:

root@timothyray:~# apt-get install nginx 
Reading package lists... Done
Building dependency tree
Reading state information... Done
E: Unable to locate package nginx
root@timothyray:~#

It is not just about nginx. I can not install any other package either.

Update your OS repository list:

touch /etc/apt/sources.list
soudo apt-get update

Your /etc/apt/sources.list is missing or corrupted. You can create file using command line tools by simply typing following commands.

Add four main repositories

sudo add-apt-repository main
sudo add-apt-repository universe
sudo add-apt-repository restricted
sudo add-apt-repository multiverse

Add security and update repositories for 18.04 LTS

sudo -- sh -c "echo 'deb http://security.ubuntu.com/ubuntu/ bionic-security multiverse main restricted universe' >> /etc/apt/sources.list"
sudo -- sh -c "echo 'deb http://archive.ubuntu.com/ubuntu bionic-updates multiverse main restricted universe' >> /etc/apt/sources.list"
sudo -- sh -c "echo 'deb http://archive.ubuntu.com/ubuntu bionic-backports multiverse main restricted universe' >> /etc/apt/sources.list"

At the end /etc/apt/sources.list should look as follows when viewed with the cat command on Linux

cat /etc/apt/sources.list

Sample file for Ubuntu 19.04 created using above commands:

deb http://archive.ubuntu.com/ubuntu bionic main universe restricted multiverse
deb http://security.ubuntu.com/ubuntu/ bionic-security multiverse main restricted universe
deb http://archive.ubuntu.com/ubuntu bionic-updates multiverse main restricted universe
deb http://archive.ubuntu.com/ubuntu bionic-backports multiverse main restricted universe

Sample file for Ubuntu 20.04 LTS desktop with additional repos:

deb http://archive.ubuntu.com/ubuntu/ focal main restricted
deb http://archive.ubuntu.com/ubuntu/ focal-updates main restricted
deb http://archive.ubuntu.com/ubuntu/ focal universe
deb http://archive.ubuntu.com/ubuntu/ focal-updates universe
deb http://archive.ubuntu.com/ubuntu/ focal multiverse
deb http://archive.ubuntu.com/ubuntu/ focal-updates multiverse
deb http://archive.ubuntu.com/ubuntu/ focal-backports main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu focal-security main restricted
deb http://security.ubuntu.com/ubuntu focal-security universe
deb http://security.ubuntu.com/ubuntu focal-security multiverse

Now update the system:

sudo apt-get update
sudo apt-get upgrade
sudo apt-get install foo
sudo apt-get install nginx

How to fix “Unable to locate package” while trying to install packages with apt or apt-get using GUI tools

The above commands works great on server. But, if you have a gnome or kde or any other Ubuntu Linux desktop installed simply try a tool called software-properties-gtk

Software tab

Type the following command to start the tool

software-properties-gtk

Make sure the “Ubuntu Software” tab displays a list of repositories or “Channels”. The four main repositories are selected as follows:

The update tab

The Updates tab is where you set when and how Ubuntu receives security updates. Make sure you select it as follows and click on the Close button.


When prompted click on the “Reload” button:
Screenshot%20from%202018-08-12%2015-45-41
The system will update cache as follows:
Screenshot%20from%202018-08-12%2015-46-30

Update the system and install packages

Once updated you are ready to install or update your system using the apt-get command or apt command:

sudo apt update
sudo apt upgrade
sudo apt install nginx

I strongly suggest that you read this page here for more info.